package com.leaderment.timatt.webmagic.scheduler;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicInteger;

import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.scheduler.DuplicateRemovedScheduler;
import us.codecraft.webmagic.scheduler.MonitorableScheduler;
import us.codecraft.webmagic.scheduler.QueueScheduler;

/** 
* 重写一个不去重的队列
* @author kerain
* @date 2018年3月22日
*/
public class MyQueueScheduler  extends DuplicateRemovedScheduler implements MonitorableScheduler{
	 private BlockingQueue<Request> queue = new LinkedBlockingQueue<Request>();
	 AtomicInteger requestCount=new AtomicInteger();

	    @Override
	    public void pushWhenNoDuplicate(Request request, Task task) {
	        //queue.add(request);
	    }

	    @Override
	    public Request poll(Task task) {
	    	//System.out.println("queue.size():"+queue.size());
	    	Request req=queue.poll();
	    	if(req!=null&&queue.size()>0){
	    		requestCount.incrementAndGet();
	    	}
	        return req;
	      
	    }

	    @Override
	    public int getLeftRequestsCount(Task task) {
	        return queue.size();
	    }

	    @Override
	    public int getTotalRequestsCount(Task task) {
	    	return requestCount.get();
	        //return getDuplicateRemover().getTotalRequestsCount(task);
	    }
	    
	    @Override
	    public void push(Request request, Task task) {
	        logger.trace("get a candidate url {}", request.getUrl());
	        queue.add(request);
	    }
}
